package com.jisf.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jisf.driver.entity.po.Coach;
import com.jisf.driver.entity.po.Student;
import com.jisf.driver.entity.po.UserRole;
import com.jisf.driver.entity.vo.AllotRolesVo;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.mapper.CoachMapper;
import com.jisf.driver.mapper.StudentMapper;
import com.jisf.driver.mapper.UserRoleMapper;
import com.jisf.driver.response.IErrorCode;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.IRoleMenuService;
import com.jisf.driver.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-04-08
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private IRoleMenuService iRoleMenuService;

    @Autowired
    private CoachMapper coachMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public IErrorCode allotRoles(AllotRolesVo allotRolesVo) {
        try {
            Integer userId = allotRolesVo.getUserId();

            List<Integer> rolesIdVo = allotRolesVo.getRolesId();
            List<Integer> rolesIdPo = userRoleMapper.selectRolesByUserId(userId);

            // 计算需要删除的角色和需要新增的角色
            iRoleMenuService.calculateInsertDelete(rolesIdVo, rolesIdPo);

            // 删除角色
            int delete = 0;
            if (rolesIdPo.size() != 0) {
                delete = userRoleMapper.deleteUserRoles(userId, rolesIdPo);
            }

            // 新增角色
            int insert = 0;
            if (rolesIdVo.size() != 0) {
                insert = userRoleMapper.insertUserRoles(userId, rolesIdVo);
            }

            for (Integer roleId : rolesIdVo) {
                if (roleId == 4) {
                    QueryWrapper<Coach> coachQueryWrapper = new QueryWrapper<>();
                    coachQueryWrapper.eq("userid", userId);
                    Long aLong = coachMapper.selectCount(coachQueryWrapper);
                    if (aLong == 0) {
                        Coach coach = new Coach();
                        coach.setUserid(userId);
                        coach.setStudentsPassNumber(0);
                        coach.setStudentsAllNumber(0);
                        coachMapper.insert(coach);
                    }
                }
                if (roleId == 5) {
                    QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
                    studentQueryWrapper.eq("userid", userId);
                    Long aLong = studentMapper.selectCount(studentQueryWrapper);
                    if (aLong == 0) {
                        Student student = new Student();
                        student.setUserid(userId);
                        student.setSubjectOneTime(0);
                        student.setSubjectTwoTime(0);
                        student.setSubjectThreeTime(0);
                        student.setSubjectFourTime(0);
                        student.setSubjectOneExam(0);
                        student.setSubjectTwoExam(0);
                        student.setSubjectThreeExam(0);
                        student.setSubjectFourExam(0);
                        studentMapper.insert(student);
                    }
                }
            }

            return delete + insert == 0 ? ResultCode.USER_ROLE_ALLOT_FAILED : ResultCode.USER_ROLE_ALLOT_SUCCESS;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }
}
